import { ElMessage, ElMessageBox } from 'element-plus'
import {
  getProductList,
  addProduct,
  editProduct,
  removeProduct,
  searchProduct
} from '@/api/product.js'

export default {
  namespaced: true,
  state: () => ({
    productList: []
  }),
  mutations: {
    getProductListMutations(state, playLoad) {
      state.productList = playLoad.data
    },
    searchProductMutations(state,playLoad){
      state.productList=playLoad
    }
  },
  actions: {
    async getProductListAction({ commit, dispatch }, info) {
      const res = await getProductList({})
      commit('getProductListMutations', { ...res })
    },
    async addSubmitActions({ commit, dispatch }, info) {
      const res = await addProduct(info)
      res.code == 0 ? ElMessage.success('成功！') : ElMessage.error('失败！')
      dispatch('getProductListAction', {})
    },
    async submitActions({ commit, dispatch }, info) {
      const res = await editProduct(info)
      res.code == 0 ? ElMessage.success('成功！') : ElMessage.error('失败！')
      dispatch('getProductListAction', {})
    },
    async switchChangeActions({ commit, dispatch },info){
        const res = await removeProduct({id:info.id})
        res.code == 0 ? ElMessage.success('成功！') : ElMessage.error('失败！')
        dispatch('getProductListAction', {})
    },
    async searchArticleActions({ commit },info){
      const res = await searchProduct({  productName: info})
      commit('getProductListMutations', res)
    }
  }
}
